import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/navigation/navigation_logic.dart';
import 'package:ball_lion/module/match/list/match_page_logic.dart';
import 'package:ball_lion/module/match/list/zq_focus_logic.dart';
import 'package:ball_lion/module/match/list/zq_match_filter_logic.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/constant.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/loading/loading_match_list.dart';
import 'package:ball_lion/module/match/soccer/zq_match_cell.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/widgets/ads_label.dart';
import 'package:ball_lion/widgets/header_delegate.dart';

import '../../../resources/colours.dart';
import '../../../widgets/base/wqs_text.dart';

class ZqFocusView extends StatefulWidget {
  const ZqFocusView({super.key});

  @override
  State<ZqFocusView> createState() => _ZqFocusViewState();
}

class _ZqFocusViewState extends State<ZqFocusView>
    with TickerProviderStateMixin {
  final logic =
      Get.put(ZqFocusLogic(ZqMatchType.focus, Constant.matchFilterTagFocus));
  final scrollC = ApmScrollController();
  @override
  void initState() {
    super.initState();
    scrollC.addListener(() {
      Get.find<NavigationLogic>().onMatchPageScroll();
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZqFocusLogic>(builder: (_) {
      return VisibilityDetector(
        key: const Key("interestKey"),
        onVisibilityChanged: (VisibilityInfo info) {
          logic.setVisible(!info.visibleBounds.isEmpty);
        },
        child: !logic.isLogin()
            ? NoDataWidget(
                tip: "登录后才能关注比赛",
                buttonText: "去登录",
                onTap: () => User.needLogin(() async {}))
            : Stack(
                children: [
                  EasyRefresh.builder(
                    controller: logic.refreshC,
                    header: Styles.refreshHeader(
                        position: IndicatorPosition.locator),
                    onRefresh: () async {
                      Utils.onEvent('bspd_sx');
                      await logic.getMatch();
                    },
                    childBuilder:
                        (BuildContext context, ScrollPhysics physics) =>
                            CustomScrollView(
                      physics: physics,
                      slivers: [
                        const HeaderLocator.sliver(),
                        SliverToBoxAdapter(
                          child: Container(
                            color: Colors.white,
                            child: Row(
                              children: [
                                Container(
                                  width: 50,
                                  height: 50,
                                  decoration: BoxDecoration(
                                    color: Colours.B1,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4)),
                                  ),
                                  margin:
                                      const EdgeInsets.fromLTRB(16, 8, 0, 8),
                                  child: Center(
                                    child: QsText("全部",
                                        fontSize: 14, color: Colours.B6),
                                  ),
                                ),

                                // 需要新增的列表
                                Expanded(
                                  child: SizedBox(
                                    height: 50,
                                    child: ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                      itemCount: 10,
                                      itemBuilder: (context, index) {
                                        return Container(
                                          width: 50,
                                          margin: const EdgeInsets.symmetric(
                                              horizontal: 6),
                                          child: Card(
                                            child: Center(),
                                          ),
                                        );
                                      },
                                    ),
                                  ),
                                ),

                                Container(
                                  width: 50,
                                  height: 66,
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    boxShadow: [
                                      BoxShadow(
                                        color: Colors.black.withOpacity(0.1),
                                        spreadRadius: 1,
                                        blurRadius: 2,
                                        offset: Offset(-2, 0),
                                      ),
                                    ],
                                  ),
                                  child: Center(
                                    child: Image.asset(
                                      Utils.getImgPath('icon_list.png'),
                                      width: 24,
                                      height: 24,
                                      fit: BoxFit.cover,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        SliverToBoxAdapter(
                            child: Padding(
                          padding: const EdgeInsets.symmetric(
                              vertical: 8.0, horizontal: 16),
                          child: Row(
                            children: [
                              Container(
                                height: 50,
                                width: 50,
                                decoration: BoxDecoration(
                                  color: Colours.listBox,
                                  borderRadius: BorderRadius.circular(4),
                                ),
                                child: QsText("关注的比赛",
                                    color: Colours.white, fontSize: 13),
                              ),
                              ListView(),
                              Container(
                                decoration: BoxDecoration(
                                  color: Colours.N1,
                                  borderRadius: BorderRadius.circular(4),
                                ),
                                child: QsText("关注的比赛",
                                    color: Colours.white, fontSize: 13),
                              )
                            ],
                          ),
                        )),
                        if (SpUtils.matchZqAds?[4].hasValue == false ||
                            DateTime.now()
                                    .difference(
                                        DateTime.parse(SpUtils.matchZqAds![4]))
                                    .inHours >
                                24)
                          SliverToBoxAdapter(
                            child: AdsLabel(
                              adsTag: "match_zq_4",
                              close: 4,
                              tapType: 'match_advertising',
                              tagParam: "关注",
                              margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
                              onClose: () => setState(() {}),
                            ),
                          ),
                        LoadingWidget<int>(
                          data: logic.originalMatch.length,
                          isLoading: logic.firstLoad,
                          noData: NoDataWidget.sliver(
                              tip: '暂无关注的比赛',
                              buttonText: '去关注',
                              onTap: () => Get.find<MatchPageLogic>()
                                  .changeMatchTab(Constant.matchFilterTagAll)),
                          loading: const LoadingMatchList.sliver(sportsId: 1),
                          child: () => const SliverToBoxAdapter(),
                        ),
                        LoadingWidget<int>(
                          data: logic.groupMatchList?.length ?? 0,
                          isLoading: logic.firstLoad,
                          loading: const LoadingMatchList.sliver(sportsId: 1),
                          noData: NoDataWidget.sliver(
                            tip: logic.quickFilter == QuickFilter.yiji
                                ? '暂无一级比赛'
                                : (logic.quickFilter == QuickFilter.jingcai
                                    ? '暂无竞足比赛'
                                    : '暂无比赛'),
                          ),
                          child: () => const SliverToBoxAdapter(),
                        ),
                        if ((logic.groupMatchList?.length ?? 0) > 0)
                          ...List.generate(
                              logic.dayList?.length ?? 0,
                              (index) => SliverMainAxisGroup(slivers: [
                                    SliverPersistentHeader(
                                        pinned: true,
                                        delegate: HeaderDelegate(
                                            extent: 28,
                                            child: Styles.matchListGroupHeader(
                                                logic.dayList![index]))),
                                    SliverList(
                                        delegate: SliverChildBuilderDelegate(
                                            childCount: logic
                                                    .groupMatchList?[index]
                                                    .length ??
                                                0,
                                            (context, i) => ZqMatchCell(
                                                logic.groupMatchList![index][i],
                                                logic)))
                                  ])),
                        SliverToBoxAdapter(
                          child: Container(height: 80),
                        )
                      ],
                    ),
                  ),
                  Styles.matchListBottomBar(
                      logic.hideMatchNum != 0 && !logic.isBottomHide,
                      logic.hideMatchNum, onClose: () {
                    Utils.onEvent('bspd_sxfc_gb');
                    logic.onCloseBottomBar();
                  }, onDefault: () {
                    Utils.onEvent('bspd_sxfc_hf');
                    logic.onSelectDefaultMatch();
                  })
                ],
              ),
      );
    });
  }

  Widget matchList() {
    return CustomScrollView(
      slivers: List.generate(
          logic.dayList?.length ?? 0,
          (index) => SliverMainAxisGroup(slivers: [
                SliverPersistentHeader(
                    pinned: true,
                    delegate: HeaderDelegate(
                        extent: 28,
                        child: Styles.matchListGroupHeader(
                            logic.dayList![index]))),
                SliverList(
                    delegate: SliverChildBuilderDelegate(
                        childCount: logic.groupMatchList?[index].length ?? 0,
                        (context, i) => ZqMatchCell(
                            logic.groupMatchList![index][i], logic)))
              ])),
    );
  }
}
